<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>订单列表</title>
<#include "../common/header.ftl"/>
    <script type="text/javascript" src="/js/plugins/jquery.form.js"></script>
    <script type="text/javascript" src="/js/plugins/jquery.twbsPagination.min.js"></script>

    <script type="text/javascript">
        $(function () {

           //商品编辑
            $("#productEdit").click(function () {
                $("#productEditform")[0].reset();
                var data = $(this).data("json");
                $("#productModal [name=id]").val(data);

                //计算商品合计
                var totalPrice = 0;//用于累加 小计的和
                var trs = $("#edit_body tr").size();
                //循环遍历tr数组，获取每一个tr
                for (var i = 0; i < trs; i++) {
                    var number = document.getElementById("number" + i).value;
                    var price = document.getElementById("price" + i).value;
                    var smallTotal = number * price;
                    totalPrice = (totalPrice + smallTotal);
                }
                //为 id=sum的td 赋值
                document.getElementById("total").innerHTML = parseFloat(totalPrice).toFixed(2);

                $("#productModal").modal("show");
            });
                //提交
            $(".btn_audit_product").click(function () {
                $("#productModal").modal("hide");
                /*//提交之前修改明细的名称,添加上索引
                //找到tbody中所有的行
                $.each($("#edit_table_body tr"), function (index,item) {
                    var index = index - 1;
                    $(item).find("[tag=id]").prop("name","orderProducts["+index+"].id");
                    $(item).find("[tag=productPrice ]").prop("name","orderProducts["+index+"].productPrice");
                    $(item).find("[tag=productNumber]").prop("name","orderProducts["+index+"].productNumber");
                });*/
                $("#productEditform").ajaxSubmit(function (data) {
                    if (data.success) {
                        $.messager.confirm("提示", "修改成功!", function () {
                            window.location.reload();
                        });
                    }
                });
                return false;
            });
                //为金额与数量绑定change事件,并计算金额小计
            $("[tag=productPrice],[tag=productNumber]").change(function () {
                var currentTr = $(this).closest("tr");
                //得到两个元素的值
                var productPrice = currentTr.find("[tag=productPrice]").val();
                var productNumber = currentTr.find("[tag=productNumber]").val();
                //设置金额小计
                if (productPrice && productNumber) {
                    //转换成小数类型
                    productPrice = parseFloat(productPrice);
                    productNumber = parseFloat(productNumber);

                    currentTr.find("[tag=amount]").html((productPrice * productNumber).toFixed(2));
                }

                //计算商品合计
                var totalPrice = 0;//用于累加 小计的和
                var trs = $("#edit_body tr").size();
                //循环遍历tr数组，获取每一个tr
                for (var i = 0; i < trs; i++) {
                    var number = document.getElementById("number" + i).value;
                    var price = document.getElementById("price" + i).value;
                    var smallTotal = number * price;
                    totalPrice = (totalPrice + smallTotal);
                }
                //为 id=sum的td 赋值
                document.getElementById("total").innerHTML = parseFloat(totalPrice).toFixed(2);
            });

        });
    </script>


</head>
<body>
<div class="container">
<#--<#include "../common/top.ftl"/>-->
    <div class="row">
    <#--<div class="col-sm-3">
        <#assign currentMenu="userFileAuth" />
        <#include "../common/menu.ftl" />
    </div>-->
        <div class="col-sm-9">
            <div class="page-header">
                <h3>商品信息</h3>
            </div>

            <div class="panel panel-default">
                <table class="table">
                    <thead>
                    <tr>
                        <th>商品名称</th>
                        <th>货号</th>
                        <th>价格</th>
                        <th>数量</th>
                        <th>属性</th>
                        <th>小计</th>
                    </tr>
                    </thead>
                    <tbody>
                    <#list products as vo>
                    <tr>
                        <td>${vo.productName}</td>
                        <td>${vo.orderId}</td>
                        <td><input type="text" name="productPrice" value="${vo.productPrice}"/></td>
                        <td><input type="text" name="productNumber" value="${vo.productNumber}"/></td>
                        <td>${vo.skuAttr}</td>
                        <td>${vo.totalAmount}</td>
                    </tr>
                    </#list>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td>总计:</td>
                        <td>xxx</td>
                    </tr>
                    </tbody>
                </table>
                <div style="text-align: center;">
                    <ul id="pagination" class="pagination"></ul>
                </div>
            </div>
        </div>
    </div>

</div>
</body>
</html>

<div class="modal fade" id="productModal" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-body">
                <form class="form-horizontal" id="productEditform" method="post" action="/orderInfo/productAmend">


                    <fieldset>
                        <div id="legend" class="">
                            <legend>商品信息</legend>

                        </div>
                        <input type="hidden" name="id" value=""/>
                        <div>
                            <table class="table">
                                <tr>
                                    <th>商品名称</th>
                                    <th>货号</th>
                                    <th>价格</th>
                                    <th>数量</th>
                                    <th>属性</th>
                                    <th>小计</th>
                                </tr>
                                <tbody id="edit_body">
                                <#list products as vo>
                                <tr>
                                    <input type="hidden" name="orderProducts[${vo_index}].id" value="${vo.id}"
                                           tag="id"/>
                                    <td>${vo.productName}</td>
                                    <td>${vo.orderId}</td>
                                    <td><input type="text" id="price${vo_index}" tag=productPrice
                                               name="orderProducts[${vo_index}].productPrice"
                                               value="${vo.productPrice?string("#.00")}" style="width:100px"/></td>
                                    <td><input type="text" id="number${vo_index}" tag=productNumber
                                               name="orderProducts[${vo_index}].productNumber"
                                               value="${vo.productNumber?string("#.##")}" style="width:100px"/></td>
                                    <td>${vo.skuAttr}</td>
                                    <td><span tag="amount">${vo.totalAmount?string("#.00")}</span></td>
                                </tr>
                                </#list>
                                </tbody>
                                <tr>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td>总计:</td>
                                    <td><span id="total"></span></td>
                                </tr>
                            </table>
                        </div>


                    </fieldset>


                </form>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-success btn_audit_product" value="1">提交</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
